﻿using Newtonsoft.Json;
using PhysicalExamination5.Interface.SystemSetting;
using PhysicalExamination5.Model.PhysicalExamination;
using PhysicalExamination5.Model.SystemSetting;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace PhysicalExamination5.Service.SystemSetting
{
    public class SCaiDanRule: ICaiDanRule
    {
        private readonly ISqlSugarClient db;
        public SCaiDanRule(ISqlSugarClient db)
        {
            this.db = db;
        }
        /// <summary>
        /// 查询全部菜单规则
        /// </summary>
        /// <returns></returns>
        public string GetCaiDanRuleAll()
        {
            int nret = 0; string strFanHuiZhi = "";
            try
            {
                Logger.Singleton.Debug($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】");
                RuLeClass ruLeClass;
                Children children;
                Meta meta;
                List<RuLeClass> lstruLeClass = new List<RuLeClass>();

                //查询是否存在首页
                var ShouYeRule = new SqlSugarRepository<XT_CaiDan_Rule>(db).getByWhere(t => t.ShangJiRuleId == 0 && t.Type == "菜单项");
                if (ShouYeRule.Count != 0)
                {
                    ruLeClass = new RuLeClass();
                    ruLeClass.iRuleId = ShouYeRule[0].RuleId;
                    ruLeClass.iShangJiRuleId = ShouYeRule[0].ShangJiRuleId;
                    ruLeClass.strType = ShouYeRule[0].Type;
                    ruLeClass.path = ShouYeRule[0].Path;
                    ruLeClass.name = ShouYeRule[0].Name;
                    ruLeClass.component = ShouYeRule[0].Component;
                    meta = new Meta();
                    meta.icon = ShouYeRule[0].Icon;
                    meta.title = ShouYeRule[0].Title;
                    meta.isLink = ShouYeRule[0].isLink;
                    meta.isHide = ShouYeRule[0].isHide;
                    meta.isFull = ShouYeRule[0].isFull;
                    meta.isAffix = ShouYeRule[0].isAffix;
                    meta.isKeepAlive = ShouYeRule[0].isKeepAlive;
                    ruLeClass.meta = meta;
                    lstruLeClass.Add(ruLeClass);
                }
                List<XT_CaiDan_Rule> listCaiDanMuLu = new SqlSugarRepository<XT_CaiDan_Rule>(db).getByWhere(t => t.Type == "菜单目录");
                foreach (XT_CaiDan_Rule CaiDanMuLu in listCaiDanMuLu)
                {
                    ruLeClass = new RuLeClass();
                    ruLeClass.iRuleId = CaiDanMuLu.RuleId;
                    ruLeClass.iShangJiRuleId = CaiDanMuLu.ShangJiRuleId;
                    ruLeClass.strType = CaiDanMuLu.Type;
                    ruLeClass.path = CaiDanMuLu.Path;
                    ruLeClass.name = CaiDanMuLu.Name;
                    ruLeClass.redirect = CaiDanMuLu.Redirect;
                    meta = new Meta();
                    meta.icon = CaiDanMuLu.Icon;
                    meta.title = CaiDanMuLu.Title;
                    meta.isLink = CaiDanMuLu.isLink;
                    meta.isHide = CaiDanMuLu.isHide;
                    meta.isFull = CaiDanMuLu.isFull;
                    meta.isAffix = CaiDanMuLu.isAffix;
                    meta.isKeepAlive = CaiDanMuLu.isKeepAlive;
                    ruLeClass.meta = meta;
                    List<XT_CaiDan_Rule> listCaiDanItem = new SqlSugarRepository<XT_CaiDan_Rule>(db).getByWhere(t => t.ShangJiRuleId == CaiDanMuLu.RuleId);
                    if (listCaiDanItem.Count > 0)
                    {
                        List<Children> listChildren = new List<Children>();
                        foreach (XT_CaiDan_Rule CaiDanItem in listCaiDanItem)
                        {
                            children = new Children();
                            children.iRuleId = CaiDanItem.RuleId;
                            children.iShangJiRuleId = CaiDanItem.ShangJiRuleId;
                            children.strType = CaiDanItem.Type;
                            children.path = CaiDanItem.Path;
                            children.name = CaiDanItem.Name;
                            children.component = CaiDanItem.Component;
                            meta = new Meta();
                            meta.icon = CaiDanItem.Icon;
                            meta.title = CaiDanItem.Title;
                            meta.isLink = CaiDanItem.isLink;
                            meta.isHide = CaiDanItem.isHide;
                            meta.isFull = CaiDanItem.isFull;
                            meta.isAffix = CaiDanItem.isAffix;
                            meta.isKeepAlive = CaiDanItem.isKeepAlive;
                            children.meta = meta;
                            listChildren.Add(children);
                        }
                        ruLeClass.children = listChildren;
                    }
                    lstruLeClass.Add(ruLeClass);
                }
              

            PROC_EXIT:
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = nret,
                    Message = strFanHuiZhi,
                    Data = lstruLeClass
                }) ;
            }
            catch (Exception ex)
            {
                Logger.Singleton.Error($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:报错异常:" + ex.Message);
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = 1000,
                    Message = ex.Message,
                    Data = ""
                });
            }
        }
        /// <summary>
        /// 查询全部菜单目录
        /// </summary>
        /// <returns></returns>
        public string GetCaiDanMuLuAll()
        {
            int nret = 0; string strFanHuiZhi = "";
            try
            {
                Logger.Singleton.Debug($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】");

                var CaiDanMuLu = new SqlSugarRepository<XT_CaiDan_Rule>(db).getByWhere(t => t.Type == "菜单目录").Select(t => new { t.RuleId, t.Title }).ToList();

            PROC_EXIT:
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = nret,
                    Message = strFanHuiZhi,
                    Data = CaiDanMuLu
                });
            }
            catch (Exception ex)
            {
                Logger.Singleton.Error($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:报错异常:" + ex.Message);
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = 1000,
                    Message = ex.Message,
                    Data = ""
                });
            }
        }
        /// <summary>
        /// 新增菜单规则
        /// </summary>
        /// <param name="caiDanRule"></param>
        /// <returns></returns>
        public string PostCaiDanRuleAdd(CaiDanRuleClass caiDanRule)
        {
            int nret = 0; string strFanHuiZhi = "";
            try
            {
                Logger.Singleton.Debug($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:接收参数【{caiDanRule}】");

                #region 数据验证

                if (caiDanRule.Validate(out strFanHuiZhi) == false) goto PROC_EXIT;

                #endregion

                //新增数据
                XT_CaiDan_Rule XT_CaiDan_Rule = new XT_CaiDan_Rule();
                XT_CaiDan_Rule.ShangJiRuleId = caiDanRule.iShangJiRuleId;
                XT_CaiDan_Rule.Type = caiDanRule.strType.Trim();
                XT_CaiDan_Rule.Title = caiDanRule.strTitle.Trim();
                XT_CaiDan_Rule.Path = caiDanRule.strPath.Trim();
                XT_CaiDan_Rule.Name = caiDanRule.strName.Trim();
                XT_CaiDan_Rule.Icon = caiDanRule.strIcon.Trim();
                XT_CaiDan_Rule.Redirect = caiDanRule.strRedirect.Trim();
                XT_CaiDan_Rule.Component = caiDanRule.strComponent.Trim();
                XT_CaiDan_Rule.isLink = caiDanRule.isLink;
                XT_CaiDan_Rule.isHide = caiDanRule.isHide;
                XT_CaiDan_Rule.isFull = caiDanRule.isFull;
                XT_CaiDan_Rule.isAffix = caiDanRule.isAffix;
                XT_CaiDan_Rule.isKeepAlive = caiDanRule.isKeepAlive;
                XT_CaiDan_Rule.CreateTime = DateTime.Now;
                if (new SqlSugarRepository<XT_CaiDan_Rule>(db).Add(XT_CaiDan_Rule) == false)
                {
                    nret = 1000;
                    strFanHuiZhi = "菜单规则新增失败!";
                    goto PROC_EXIT;
                }


            PROC_EXIT:
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = nret,
                    Message = strFanHuiZhi,
                    Data =""
                });
            }
            catch (Exception ex)
            {
                Logger.Singleton.Error($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:报错异常:" + ex.Message);
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = 1000,
                    Message = ex.Message,
                    Data = ""
                });
            }
        }
        /// <summary>
        /// 修改菜单规则
        /// </summary>
        /// <param name="caiDanRule"></param>
        /// <returns></returns>
        public string PutCaiDanRuleUpdate(CaiDanRuleClass caiDanRule)
        {
            int nret = 0; string strFanHuiZhi = "";
            try
            {
                Logger.Singleton.Debug($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:接收参数【{caiDanRule}】");

                #region 数据验证

                if (caiDanRule.Validate(out strFanHuiZhi) == false) goto PROC_EXIT;

                #endregion

                //修改数据
                XT_CaiDan_Rule XT_CaiDan_Rule = new XT_CaiDan_Rule();
                XT_CaiDan_Rule.RuleId = caiDanRule.iRuleId;
                XT_CaiDan_Rule.ShangJiRuleId = caiDanRule.iShangJiRuleId;
                XT_CaiDan_Rule.Type = caiDanRule.strType.Trim();
                XT_CaiDan_Rule.Title = caiDanRule.strTitle.Trim();
                XT_CaiDan_Rule.Path = caiDanRule.strPath.Trim();
                XT_CaiDan_Rule.Name = caiDanRule.strName.Trim();
                XT_CaiDan_Rule.Icon = caiDanRule.strIcon.Trim();
                XT_CaiDan_Rule.Redirect = caiDanRule.strRedirect.Trim();
                XT_CaiDan_Rule.Component = caiDanRule.strComponent.Trim();
                XT_CaiDan_Rule.isLink = caiDanRule.isLink;
                XT_CaiDan_Rule.isHide = caiDanRule.isHide;
                XT_CaiDan_Rule.isFull = caiDanRule.isFull;
                XT_CaiDan_Rule.isAffix = caiDanRule.isAffix;
                XT_CaiDan_Rule.isKeepAlive = caiDanRule.isKeepAlive;
                XT_CaiDan_Rule.UpdateTime = DateTime.Now;
                if (new SqlSugarRepository<XT_CaiDan_Rule>(db).Update(XT_CaiDan_Rule) == false)
                {
                    nret = 1000;
                    strFanHuiZhi = "菜单规则修改失败!";
                    goto PROC_EXIT;
                }


            PROC_EXIT:
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = nret,
                    Message = strFanHuiZhi,
                    Data = ""
                });
            }
            catch (Exception ex)
            {
                Logger.Singleton.Error($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:报错异常:" + ex.Message);
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = 1000,
                    Message = ex.Message,
                    Data = ""
                });
            }
        }
        /// <summary>
        /// 删除菜单规则
        /// </summary>
        /// <param name="ruleId"></param>
        /// <returns></returns>
        public string DeleteCaiDanRuleDelete(RuleId ruleId)
        {
            int nret = 0; string strFanHuiZhi = "";
            try
            {
                Logger.Singleton.Debug($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:接收参数iRuleId:【{ruleId.iRuleId}】");

                if (new SqlSugarRepository<XT_CaiDan_Rule>(db).getByWhere(t => t.RuleId == ruleId.iRuleId).Count == 0)
                {
                    nret = 1;
                    strFanHuiZhi = $@"当前ID【{ruleId.iRuleId}】没有查询到记录!";
                    goto PROC_EXIT;
                }

                if (new SqlSugarRepository<XT_CaiDan_Rule>(db).getByWhere(t => t.ShangJiRuleId == ruleId.iRuleId).Count > 0)
                {
                    nret = 1;
                    strFanHuiZhi = "当前菜单目录下存在菜单项不可删除!";
                    goto PROC_EXIT;
                }


                if (new SqlSugarRepository<XT_CaiDan_Rule>(db).DeleteWhere(t => t.RuleId == ruleId.iRuleId) ==false)
                {
                    nret = 1000;
                    strFanHuiZhi = "菜单规则删除失败!";
                    goto PROC_EXIT;
                }



                PROC_EXIT:
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = nret,
                    Message = strFanHuiZhi,
                    Data = ""
                });
            }
            catch (Exception ex)
            {
                Logger.Singleton.Error($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:报错异常:" + ex.Message);
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = 1000,
                    Message = ex.Message,
                    Data = ""
                });
            }
        }
    }
}
